<template>
    <div id="bg">
      <div class="register-content">
        <div class="register_form">
          <h1>学生管理系统 | 注册</h1>
          <el-form
            :model="registerInfo"
            status-icon
            ref="registerInfo"
            class="demo-registerInfo"
          >
            <el-form-item
              prop="username"
              :rules="[{ required: true, message: '用户名不能为空' }]"
            >
              <el-input
                v-model="registerInfo.username"
                @focus="isUserChecked = true"
                @blur="aaa"
              ></el-input>
              <div :class="isUserChecked == true ? 'checked' : 'username'">
                用户名
              </div>
            </el-form-item>
            <el-form-item
              prop="password"
              :rules="[{ required: true, message: '密码不能为空' }]"
              class="input_martop"
            >
              <el-input
                type="password"
                v-model="registerInfo.password"
                autocomplete="off"
                @focus="isPassChecked = true"
                @blur="bbb"
              ></el-input>
              <div :class="isPassChecked ? 'checked' : 'password'">密码</div>
            </el-form-item>
            <el-form-item
              prop="name"
              :rules="[{ required: true, message: '姓名不能为空' }]"
              class="input_martop"
            >
              <el-input
                v-model="registerInfo.name"
                @focus="isNameChecked = true"
                @blur="ccc"
              ></el-input>
              <div :class="isNameChecked == true ? 'checked' : 'name'">
                姓名
              </div>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('registerInfo')"
                >完成</el-button
              >
              <el-button @click="login()">已有账号，去登陆</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
</template>
<style>
#bg {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(../../login-bg.jpg) no-repeat;
  background-size: cover;
}
.register-content {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: -350px;
  width: 900px;
  height: 700px;
  /* line-height: 700px; */
  background-color: #fff;
  border-radius: 50%;
}
.register_form {
  width: 400px;
  text-align: center;
  /* margin: auto; */
  margin-left: 80px;
  margin-top: 50%;
  transform: translateY(-80%);
}
.register_form h1 {
  margin-bottom: 70px;
}
.input_martop {
  margin-top: 50px;
}
.username,
.password,
.name {
  position: absolute;
  top: 0;
  left: 20px;
  pointer-events: none;
  transition: 200ms;
}
.checked {
  position: absolute;
  top: -35px;
  left: 3px;
  font-size: 18px;
  font-weight: 500;
  color: rgb(43, 46, 236);
  transition: 200ms;
}
</style>
<script>
import { getRegister } from "../../api/register";
export default {
  name: "Register",
  data() {
    return {
      registerInfo: {
        username: "",
        password: "",
        name: "",
      },
      isUserChecked: false,
      isPassChecked: false,
      isNameChecked: false,
    };
  },
  methods: {
    aaa() {
      if (this.registerInfo.username == "") {
        this.isUserChecked = false;
      }
    },
    bbb() {
      if (this.registerInfo.password == "") {
        this.isPassChecked = false;
      }
    },
    ccc() {
      if (this.registerInfo.name == "") {
        this.isNameChecked = false;
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          getRegister(this.registerInfo).then((res) => {
            console.log(res.data);
            if (res.data.success === true) {
              this.$message({
                message: "操作" + res.data.msg,
                type: "success",
              });
              this.$router.replace("/");
            } else {
              this.$message.error(res.data.data.msg);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    login() {
      this.$router.push("/Login");
    },
  },
};
</script>
